#include "gan.h"
#include <QTimer>
#include <cmath>
using namespace std;
Gan::~Gan()
{

}
Gan::Gan(double a,double b, double c,double d,Ui::painter* ui_ptr)
{
    this->ui = ui_ptr;
    this->LengthGan1 =a;
    this->LengthGan2 =b;
    this->LengthGan3 =c;
    this->LengthGan4 =d;
    this->ui->OutputText->append("检查机构参数合法性及类型");
    return;
}
void Gan::Judge()
{
    if (this->Min_Max_Bar_sum > this->Other_Bar_sum)
    {
        this->ui->OutputText->append("四杆机构类型：双摇杆机构");
        this->ui->MechanisimType->setText(QString("双摇杆机构"));
        return;
    }
    else
    {
    if (this->Min_Bar_Num == 3)
    {
    this->ui->OutputText->append("四杆机构类型：双曲柄机构");
    this->ui->MechanisimType->setText(QString("双曲柄机构"));
    return;
    }
    else if (this->Min_Bar_Num == 1 || this->Min_Bar_Num == 2)
    {
        this->ui->OutputText->append("四杆机构类型：曲柄摇杆机构");
        this->ui->MechanisimType->setText(QString("曲柄摇杆机构"));
        return;
    }
    else if (this->Min_Bar_Num == 0)
    {
        this->ui->OutputText->append("四杆机构类型：双摇杆机构");
        this->ui->MechanisimType->setText(QString("双摇杆机构"));
        return;
    }
    }
}
bool Gan::CheckInput()
{
    for (int i = 0; i < 4; i++)
    {
    if (this->Bar[i] <= 0)
    {
        this->ui->OutputText->append("输入错误:输入的杆存在小于或等于零的杆");
        this->isInputLeagal = false;
        return false;
    }
    else
    {
        this->isInputLeagal = true;
    }
    }
    return true;
}
void Gan::MaxBar(float *Bar)
{
    float tmp = Bar[0];
    //求出最长的杆的长度以及其所处的位置
    for (int i = 1; i <= 3; i++)
    {
    if (tmp < Bar[i])
    {
    tmp = Bar[i];
    this->Max_Bar_Num = i;
    }
    }
    this->Max_Bar_length = tmp;
}
void Gan::OtherBarSum()
{
    this->Min_Max_Bar_sum = this->Max_Bar_length + this->Min_Bar_Length;//其它两杆的长度和
    this->Other_Bar_sum = 0;
    int flag = 1;
    for (int i = 0; i < 4; i++)
    {

    if (i == this->Max_Bar_Num || i == this->Min_Bar_Num)
    {
    continue;
    }
    else
    {
    this->Other_Bar_num[flag] = i;
    this->Other_Bar_sum += this->Bar[i];
    }
    }
}
void Gan::MinBar(float *Bar)
{
    float tmp = Bar[0];
    //求出最短的杆的长度以及其所处的位置
    for (int i = 1; i <= 3; i++)
    {
    if (tmp > Bar[i])
    {
    tmp = Bar[i];
    this->Min_Bar_Num = i;
    }
    }
    this->Min_Bar_Length = tmp;
}

void Gan::Check()
{
//    cout << "ab为连架杆，bc为连杆，cd也为连架杆，ad为机架。" << endl;
//    cout << "请依次输入ab、bc、cd、ad各杆长长度：" << endl;
//    cin >> Bar[0] >> Bar[1] >> Bar[2] >> Bar[3];
    this->Bar[0] = this->LengthGan1;
    this->Bar[1] = this->LengthGan2;
    this->Bar[2] = this->LengthGan3;
    this->Bar[3] = this->LengthGan4;
    if (CheckInput())
    {
    this->MinBar(Bar);
    this->MaxBar(Bar);
    this->OtherBarSum();
    if(this->Min_Bar_Length+this->Max_Bar_length > this->Other_Bar_sum)
    {
        this->isInputLeagal = false;
        this->ui->OutputText->append("输入参数杆长条件不满足要求！");
    }
    this->Judge();

    }
}
